#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Created on 2011-11-29

@author: zhongfeng

'''
import os, time
import zipfile
from ftplib import FTP,error_perm
import sys,glob
from proxysock import setup_http_proxy
from spiderconfigparser import SpiderConfig

ip = '58.64.204.70'
port = '21'
username = '55bigo'
passwd = '55bigoadmin'

def rename(fFullName):
    t = time.strftime("%Y-%m-%d",time.gmtime(os.path.getmtime(fFullName)))
    print 'old file:%s' % fFullName
    new = '.'.join((fFullName,t))
    print 'new file:%s' % new
    try:
        if os.path.exists(new):
            os.remove(new)
        os.rename(fFullName, new)
    except Exception ,e:
        print e
        raise e
    return new

def fileUpload(remoteErrPath = r'/opt/errlog',remoteLogPath = r'/opt/log'):
    proxy = SpiderConfig.getProxy()
    if proxy :
        proxyIp = proxy.split(':')[0]
        proxyPort = proxy.split(':')[1]
        setup_http_proxy(proxyIp,proxyPort)
    curpath = os.path.abspath(os.path.dirname(sys.argv[0]))
    ret = glob.glob1(curpath,r'*_spider.log')
    siteName = None
    if ret:
        logFileName = ret[0]
        siteName = logFileName.split('_')[0]
        fullLogF = os.path.join(curpath,logFileName)
        newLogFile = rename(fullLogF)
        rlogPath = '/'.join((remoteLogPath,siteName))
        ftpupload(newLogFile,rlogPath)
        print 'upload success '
                
        #移动到当前的log目录下
        print 'backup >>>>>'
        logDir = os.path.join(curpath,'log')
        if not os.path.exists(logDir):
            os.mkdir(logDir)
        backUpLogPath = os.path.join(logDir,os.path.split(newLogFile)[-1])
        print 'move %s to %s' % (newLogFile,backUpLogPath)
        if os.path.exists(backUpLogPath):
            os.remove(backUpLogPath)
        os.rename(newLogFile, backUpLogPath)
        
    errReport = os.path.join(curpath,r'err_report.log')
    if siteName and os.path.exists(errReport):
        newErrLogName = rename(errReport)
        rerrLogPath = '/'.join((remoteErrPath,siteName))
        ftpupload(newErrLogName,rerrLogPath)
                
        #移动到当前的errlog目录下
        errlogDir = os.path.join(curpath,'errlog')
        if not os.path.exists(errlogDir):
            os.mkdir(errlogDir)
        backUpErrLogPath = os.path.join(errlogDir,os.path.split(newErrLogName)[-1])
        print 'move %s to %s' % (newErrLogName,backUpErrLogPath)
        if os.path.exists(backUpErrLogPath):
            os.remove(backUpErrLogPath)
        os.rename(newErrLogName, backUpErrLogPath)
    
def ftpupload(filename,remotePath):
    ftp = FTP()
    ftp.set_debuglevel(2)
    ftp.connect(ip, port)
    ftp.login(username, passwd)
    try:
        ftp.cwd(remotePath)
    except:
        try:
            ftp.mkd(remotePath)
        except error_perm: 
            print 'U have no authority to make dir'
    bufsize = 8192
    file_handler = open(filename, 'rb')
    ftp.storbinary('STOR %s' % os.path.basename(filename), file_handler, bufsize)
    ftp.set_debuglevel(0)
    file_handler.close()
    ftp.quit()
#===============================================================================
# 
# class ZFile(object):
#    def __init__(self, filename, mode='r', basedir=''):
#        self.filename = filename
#        self.mode = mode
#        if self.mode in ('w', 'a'):
#            self.zfile = zipfile.ZipFile(filename, self.mode, compression=zipfile.ZIP_DEFLATED)
#            print self.zfile.filename
#        else:
#            self.zfile = zipfile.ZipFile(filename, self.mode)
#        self.basedir = basedir
#        if not self.basedir:
#            self.basedir = os.path.dirname(filename)
#           
#    def addfile(self, path, arcname=None):
#        path = path.replace('\\', '/')
#        if not arcname:
#            if path.startswith(self.basedir):
#                arcname = path[len(self.basedir):]
#            else:
#                arcname = ''
#        self.zfile.write(path, arcname)
#               
#    def addfiles(self, paths):
#        for path in paths:
#            if isinstance(path, tuple):
#                self.addfile(*path)
#            else:
#                self.addfile(path)
#                 
#    def close(self):
#        self.zfile.close()
#           
#    def extract_to(self, path):
#        for p in self.zfile.namelist():
#            self.extract(p, path)
#               
#    def extract(self, filename, path):
#        if not filename.endswith(''):
#            f = os.path.join(path, filename)
#            dir = os.path.dirname(f)
#            if not os.path.exists(dir):
#                os.makedirs(dir)
#            file(f, 'wb').write(self.zfile.read(filename))
#               
#           
# def create(zfile, files):
#    z = ZFile(zfile, 'w')
#    z.addfiles(files)
#    z.close()
#       
# def extract(zfile, path):
#    z = ZFile(zfile)
#    z.extract_to(path)
#    z.close()
#===============================================================================


if __name__ == '__main__':
    fileUpload()